{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install PyMuPDF requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "\n",
    "sys.path.append('../aisuite')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import aisuite as ai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "def configure_environment(additional_env_vars=None):\n",
    "    \"\"\"\n",
    "    Load environment variables from .env file and apply any additional variables.\n",
    "    :param additional_env_vars: A dictionary of additional environment variables to apply.\n",
    "    \"\"\"\n",
    "    # Load from .env file if available\n",
    "    load_dotenv(find_dotenv())\n",
    "\n",
    "    # Apply additional environment variables\n",
    "    if additional_env_vars:\n",
    "        for key, value in additional_env_vars.items():\n",
    "            os.environ[key] = value\n",
    "\n",
    "# Define additional API keys and credentials\n",
    "additional_keys = {}\n",
    "\n",
    "# Configure environment\n",
    "configure_environment(additional_env_vars=additional_keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded and extracted text from pdf.\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import fitz\n",
    "from io import BytesIO\n",
    "\n",
    "# Link to paper in pdf format on the cost of avocados.\n",
    "pdf_path = \"https://arxiv.org/pdf/2104.04649\"\n",
    "pdf_text = \"\"\n",
    "# Download PDF and load it into memory\n",
    "response = requests.get(pdf_path)\n",
    "if response.status_code == 200:\n",
    "    pdf_data = BytesIO(response.content)  # Load PDF data into BytesIO\n",
    "    # Open PDF from memory using fitz\n",
    "    with fitz.open(stream=pdf_data, filetype=\"pdf\") as pdf:\n",
    "        text = \"\"\n",
    "        for page_num in range(pdf.page_count):\n",
    "            page = pdf[page_num]\n",
    "            pdf_text += page.get_text(\"text\")  # Extract text\n",
    "            pdf_text += \"\\n\" + \"=\"*50 + \"\\n\"  # Separator for each page\n",
    "    print(\"Downloaded and extracted text from pdf.\")\n",
    "else:\n",
    "    print(f\"Failed to download PDF: {response.status_code}\")\n",
    "\n",
    "question = \"Is the price of organic avocados higher than non-organic avocados? What has been the trend?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import aisuite as ai\n",
    "client = ai.Client()\n",
    "messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You are a helpful assistant. Answer the question only based on the below text.\"},\n",
    "    {\"role\": \"user\", \"content\": f\"Answer the question based on the following text:\\n\\n{pdf_text}\\n\\nQuestion: {question}\\n\"},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Based on the information provided in the text, yes, the price of organic avocados is consistently higher than conventional (non-organic) avocados. Specifically:\n",
      "\n",
      "1. Figure 2 shows a bar chart comparing average prices of conventional and organic avocados from 2015-2020. The text states that \"the average price of organic avocados is generally always higher than conventional avocados.\"\n",
      "\n",
      "2. Figure 3, a pie chart, illustrates that \"Nearly 58% of organic avocado sales averaged $1.80 per avocado and roughly 42% of conventional avocados averaged $1.30 per avocado.\"\n",
      "\n",
      "3. In the conclusion section, the text explicitly states: \"The price of organic avocados is on average 35-40% higher than conventional avocados.\"\n",
      "\n",
      "Regarding the trend, while the text doesn't provide detailed information on price trends over time, Figure 2 shows the average prices for both organic and conventional avocados from 2015-2020, indicating that this price difference has been consistent over that period.\n"
     ]
    }
   ],
   "source": [
    "anthropic_claude_3_opus = \"anthropic:claude-3-5-sonnet-20240620\"\n",
    "response = client.chat.completions.create(model=anthropic_claude_3_opus, messages=messages)\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, according to the analysis presented in the text, the price of organic avocados is higher\n"
     ]
    }
   ],
   "source": [
    "\n",
    "hf_model = \"huggingface:mistralai/Mistral-7B-Instruct-v0.3\"\n",
    "response = client.chat.completions.create(model=hf_model, messages=messages)\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "According to the text, yes, the price of organic avocados is on average 35-40% higher than conventional avocados.\n",
      "\n",
      "As for the trend, it can be observed that there is a steady growth in sales volume year after year for both conventional and organic avocados.\n",
      "\n",
      "However, in terms of price, the average price of organic avocados has been consistently higher than conventional avocados over the years. This can also be seen in Figure 2, which shows that the average price of organic avocados is generally always higher than conventional avocados.\n"
     ]
    }
   ],
   "source": [
    "fireworks_model = \"fireworks:accounts/fireworks/models/llama-v3p2-3b-instruct\"\n",
    "response = client.chat.completions.create(model=fireworks_model, messages=messages, temperature=0.75, presence_penalty=0.5, frequency_penalty=0.5)\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
